import 'package:flutter/material.dart';

class HousePerson extends StatefulWidget {
  final int selectIndex;// 外部传入默认选择第几个
  final Function slideAction; // 滚动停止的回调方法，给外部传选中的索引值
  bool clearData; // 支持清楚数据功能，true代表恢复默认样式
  HousePerson({this.slideAction, this.selectIndex=0, this.clearData=false});
  @override
  _HousePersonState createState() => _HousePersonState();
}

class _HousePersonState extends State<HousePerson> {
  List<String> _list = ['1人', '2人', '3人', '4人', '5人', '6人', '7人', '8人', '9人', '10人',];

  bool isScrollEndNotification = false;//是滚动结束通知
  ScrollController _controller;//滚动控制器
  double _startLocation = 0;//开始位置
  double _endLocation = 0;//结束位置
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void didChangeDependencies() {
    //是否更改了依赖项
    super.didChangeDependencies();
    double screenWidth = MediaQuery.of(context).size.width;//获取屏幕宽度
    int select = widget.selectIndex > 0 ? widget.selectIndex : 0;//如果index大于0，则用index，否则返回0
    _controller = ScrollController(
      initialScrollOffset: (3000+select) * (screenWidth) / 7,//初始滚动偏移
    );
  }

  @override
  void dispose() {
    //为了避免内存泄露，需要调用_controller.dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;//屏幕宽度
    double singleItemWidth = screenWidth/7;
    if(widget.clearData) {
      //是否清除数据
      _controller.jumpTo(3000 * (screenWidth) / 7);//偏移到中间
      widget.clearData = false;//清除数据
    }

    return Container(
      child: Column(
        children: <Widget>[
          Container(
            color: Colors.grey,
            width: screenWidth,
            height: 100,
            child: Stack(
              children: <Widget>[
                NotificationListener<ScrollNotification>(
                  child: ListView.builder(
                    controller: _controller,
                    itemCount: _list.length * 10000,
                    itemExtent: singleItemWidth,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        alignment: Alignment.center,
                        width: singleItemWidth,
                        child: Text(
                          _list[index % _list.length],
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 12,
                          ),
                        ),
                      );
                    },
                    scrollDirection: Axis.horizontal,
                  ),
                  onNotification: (ScrollNotification notification) {
                    if(notification is ScrollStartNotification) {
                      isScrollEndNotification = false;
                      _startLocation = notification.metrics.pixels;
                    }
                    if (notification is ScrollEndNotification && !isScrollEndNotification) {
                      _endLocation = notification.metrics.pixels;
                      isScrollEndNotification = true;
                      double differ = _endLocation-_startLocation;
                      double offset = 0;
                      if(differ>0) {
                        offset = (differ.abs()~/singleItemWidth)*singleItemWidth;
                        if(differ%singleItemWidth >= singleItemWidth/2) {
                          offset += singleItemWidth;
                        }
                        _controller.jumpTo(_startLocation + offset);
                      } else if(differ<0){
                        differ = differ.abs();
                        offset = ((differ~/singleItemWidth)*singleItemWidth);
                        if((differ%singleItemWidth) >= (singleItemWidth/2)) {
                          offset += singleItemWidth;
                        }
                        _controller.jumpTo(_startLocation - offset);
                      }
                    }
                    double result = notification.metrics.pixels/singleItemWidth;
                    int round = result.round();// 四舍五入
                    print('round${round}');
                    // widget.slideAction(round%10);// 取余之后返回索引
                    return true;
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}